﻿using System.IO;
using System.Text;
using System.Web;

namespace System
{
    public static  class LogError
    {
        private static readonly object FileLock = new object();
        public static void Log(this string message, string fileName="")
        {
            try
            {
                string path = string.Empty;
                if (string.IsNullOrEmpty(fileName))
                {
                    path = Path.Combine(HttpRuntime.AppDomainAppPath, "Logs",string.Format("{0}Log.txt", DateTime.Now.ToString("yyMMdd")));
                }
                else
                {
                    path = Path.Combine(HttpRuntime.AppDomainAppPath, "Logs", string.Format("{0}Log.txt", fileName));
                }
                lock (FileLock)
                {
                    Directory.CreateDirectory(path: Path.GetDirectoryName(path));
                    using (var stream = File.Open(path, FileMode.Append, FileAccess.Write))
                    {
                        var buffer =
                            Encoding.UTF8.GetBytes(
                                string.Format("操作时间:{0}\r\n\r\n日志信息{1}\r\n==============================\r\n\r\n",
                                              DateTime.Now, message));
                        stream.Write(buffer, 0, buffer.Length);
                        stream.Flush();
                    }
                }
            }
            finally
            {
            }
        }
        public static void Error(this Exception ex, string otherInfo,string fileName)
        {
            try
            {
                string path = Path.Combine(HttpRuntime.AppDomainAppPath, "Logs", string.Format("{0}.txt", fileName));
                lock (FileLock)
                {
                    Directory.CreateDirectory(path: Path.GetDirectoryName(path));
                    using (var stream = File.Open(path, FileMode.Append, FileAccess.Write))
                    {
                        var buffer =
                            Encoding.UTF8.GetBytes(
                                string.Format("出错时间:{0}\r\n{1}\r\n错误信息{2}\r\n==============================\r\n\r\n",
                                              DateTime.Now, otherInfo, ex));
                        stream.Write(buffer, 0, buffer.Length);
                        stream.Flush();
                    }
                }
            }
            finally
            {
            }

        }

        public static void Error(this Exception ex,string otherInfo="")
        {
            try
            {
                string path = Path.Combine(HttpRuntime.AppDomainAppPath, "Logs",
                                           string.Format("{0}Error.txt", DateTime.Now.ToString("yyMMdd")));
                lock (FileLock)
                {
                    Directory.CreateDirectory(path: Path.GetDirectoryName(path));
                    using (var stream = File.Open(path, FileMode.Append, FileAccess.Write))
                    {
                        var buffer =
                            Encoding.UTF8.GetBytes(
                                string.Format("出错时间:{0}\r\n{1}\r\n错误信息{2}\r\n==============================\r\n",
                                              DateTime.Now, otherInfo, ex.ToString()));
                        stream.Write(buffer, 0, buffer.Length);
                        stream.Flush();
                    }
                }
            }
            catch
            {
            }
            finally
            {
            }
        }
    }
    
}